package io.flutter.plugins;

import android.app.Activity;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;

import io.flutter.plugin.common.EventChannel;
import io.flutter.plugin.common.EventChannel.EventSink;
import io.flutter.plugin.common.EventChannel.StreamHandler;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugin.common.StandardMessageCodec;
import io.flutter.plugin.common.BasicMessageChannel;
import io.flutter.plugins.entity.*;

import com.ithaca.archime.MainActivity;
import com.xiaomi.mimc.MIMCMessage;

import java.lang.ref.WeakReference;


public class MimcPlugin implements MimcMsgHandleInterface, MethodChannel.MethodCallHandler {

    public static String CHANNEL = "com.ithaca.archime/mimc";

    static MethodChannel channel;

    private Activity activity;

    ResultHandler resultHandler = new ResultHandler(this);
    private static BasicMessageChannel<Object> rcvSender;

    private MimcPlugin(Activity activity) {
        this.activity = activity;
    }

    public static void registerWith(PluginRegistry.Registrar registrar) {
        channel = new MethodChannel(registrar.messenger(), CHANNEL);
        MimcPlugin instance = new MimcPlugin(registrar.activity());
        //setMethodCallHandler在此通道上接收方法调用的回调
        channel.setMethodCallHandler(instance);

        rcvSender = new BasicMessageChannel<>(registrar.messenger(), CHANNEL, new StandardMessageCodec());
    }

    @Override
    public void onMethodCall(MethodCall call, MethodChannel.Result result) {
        try {
            if (call.method.equals("sendRegisterMsg")) {
                String strAccount = call.argument("account");
                String strUid = call.argument("uid");
                String strPassword = call.argument("password");

                Msg msg = new Msg();
                msg.setMsgType(0);
                msg.setUid(strUid);
                msg.setPassword(strPassword);

                String userId = MainActivity.getUserId();
                MimcServer mimcServer = MimcServer.getInstanceForRegister(userId);
                mimcServer.addMsgHandle(this);
                mimcServer.sendMessage(strAccount, msg);

                //返回给flutter的参数
                result.success("success");
            } else if (call.method.equals("deleteInstanceForRegister")) {
                MimcServer.deleteInstanceForRegister();
                result.success("success");
            } else if (call.method.equals("sendLoginMsg")) {
                String strAccount = call.argument("account");

                Msg msg = new Msg();
                msg.setMsgType(1);
                msg.setUid(call.argument("uid"));
                msg.setPassword(call.argument("password"));

                System.out.println("android sendLoginMsg");

                String userId = MainActivity.getUserId();
                MimcServer mimcServer = MimcServer.getInstance(userId);
                mimcServer.addMsgHandle(this);
                mimcServer.sendMessage(strAccount, msg);

                //返回给flutter的参数
                result.success("success");
            }else if (call.method.equals("sendSingleMsg")) {
                Msg msg = new Msg();
                msg.setMsgType(1);

                String strUid = call.argument("uid");
                String strAccount = call.argument("friend_uid");
                MimcServer mimcServer = MimcServer.getInstance(strUid);
                mimcServer.addMsgHandle(this);
                mimcServer.sendMessage(strAccount, msg);

                //返回给flutter的参数
                result.success("success");
            } else {
                result.notImplemented();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    void send2Flutter(String pkg) {
        rcvSender.send(pkg);
    }

    /**
     * 单聊消息回调
     */
    @Override
    public void handleMsg(String pkg) {
        Message message = new Message();
        message.what = 200;
        message.obj = pkg;
        resultHandler.sendMessage(message);
    }

    /**
     * 群聊消息回调
     */
    @Override
    public void handleUnlimitGroupMsg(String pkg) {

    }

    static class ResultHandler extends Handler {
        WeakReference<MimcPlugin> fragmentRef;

        ResultHandler(MimcPlugin activity) {
            fragmentRef = new WeakReference<>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            MimcPlugin theActivity = fragmentRef.get();
            String pkg = (String) msg.obj;
            theActivity.send2Flutter(pkg);
        }
    }

}